package com.hsy.crm.web.service.impl;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Vendor;
import com.hsy.crm.web.domain.entity.VendorType;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.vendorType.VendorTypeSelectPo;
import com.hsy.crm.web.domain.vo.vendorType.VendorTypeSelectVo;
import com.hsy.crm.web.mapper.VendorMapper;
import com.hsy.crm.web.mapper.VendorTypeMapper;
import com.hsy.crm.web.service.base.BaseService;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.UuidUtils;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Vendor;
import com.hsy.crm.web.domain.entity.VendorType;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.vendorType.VendorTypeSelectPo;
import com.hsy.crm.web.domain.vo.vendorType.VendorTypeSelectVo;
import com.hsy.crm.web.mapper.VendorMapper;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VendorTypeService extends BaseService<VendorType> {

    @Autowired
    VendorTypeMapper vendorTypeMapper;

    @Autowired
    VendorMapper vendorMapper;

    public Unified<List<VendorTypeSelectPo>> find(VendorTypeSelectVo vendorTypeSelectVo) {
        Unified<List<VendorTypeSelectPo>> unified = new Unified<>();
        unified.setData(vendorTypeMapper.find(vendorTypeSelectVo));
        return unified;
    }

    @Transactional
    public Unified<Integer> insertVendorType(VendorType vendorType) {
        Unified<Integer> unified = new Unified<>();

        checkInsert(vendorType);

        vendorType.setId(UuidUtils.getUuid());
        vendorType.setCreateTime(DateUtils.millis());
        vendorType.setCreateUserId(vendorType.getLoginUserId());
        if(vendorType.getParentId()==null){
            vendorType.setParentId(-1L);
        }

        unified.setData(vendorTypeMapper.insertVendorType(vendorType));
        return unified;
    }



    public Unified<Integer> deleteVendorType(VendorType vendorType) {
        Unified<Integer> unified = new Unified<>();

        int result = deleteCheck(vendorType);
        if( result != App.Result.SUCCESS ){
            unified.setResult(result);
            return unified;
        }
        this.delete(vendorType);
        return unified;
    }

    public Unified<Integer> updateVendorType(VendorType vendorType) {

        Integer count = vendorTypeMapper.updateCheck(vendorType);
        if( count != null ){
            throw new ServiceException(App.BussError.LEVEL_NAME_REPEAT.getCode());
        }

        return Unified.newInstance(update(vendorType));
    }

    private void checkInsert(VendorType productType){
        //check name
        VendorType checkType = new VendorType();
        checkType.setParentId(productType.getParentId());
        checkType.setName(productType.getName());
        checkType.setCustomerId(productType.getCustomerId());
        checkType.setCompanyId(productType.getLoginCompanyId());
        int count = vendorTypeMapper.selectCount(checkType);
        if( count > 0 ){
            throw new ServiceException(App.BussError.LEVEL_NAME_REPEAT.getCode());
        }
    }

    private int deleteCheck(VendorType vendorType){

        // has children
        VendorType checkType = new VendorType();
        checkType.setParentId(vendorType.getId());
        checkType.setCustomerId(vendorType.getCustomerId());
        int count = vendorTypeMapper.selectCount(checkType);
        if( count > 0 ){
            return App.BussError.DELETE_CHILD.getCode();
        }

        // has product
        Vendor entity = new Vendor();
        entity.setVendorTypeId(vendorType.getId());
        entity.setCustomerId(vendorType.getCustomerId());
        count = vendorMapper.selectCount(entity);
        if( count > 0 ){
            return App.BussError.VENDOR_TYPE_DELETE_VENDOR.getCode();
        }

        return App.Result.SUCCESS;
    }

}
